package backTrack;

import java.util.ArrayList;
import java.util.List;

/**
 * 组合数
 */
public class P77 {
    public List<List<Integer>> combine(int n, int k) {
        List<List<Integer>> total = new ArrayList<>();
        backTrack(n, k, 1, new ArrayList<>(), total);
        return total;
    }

    private void backTrack(int n, int k, int begin, List<Integer> sub, List<List<Integer>> total) {
        if (k == 0) {
            total.add(new ArrayList<>(sub));
        } else {
            for (int i = begin; i <= n; i++) {
                sub.add(i);
                backTrack(n, k - 1, i + 1, sub, total);
                sub.remove(sub.size() - 1);
            }
        }
    }

    public static void main(String[] args) {
        for (List<Integer> integers : new P77().combine(4, 2)) {
            System.out.println(integers);
        }
        ;
    }
}
